// 4 Values Whose Sum is Zero, ACM/ICPC SWERC 2005, UVa 1152
// 陈锋
#include <algorithm>
#include <cstdio>
using namespace std;

const int NN = 4000 + 8;
int A[NN], B[NN], C[NN], D[NN], S[NN * NN];
int main() {
  int T;
  scanf("%d", &T);
  for (int n; T--;) {
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
      scanf("%d%d%d%d", &A[i], &B[i], &C[i], &D[i]);
    int c = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        S[c++] = A[i] + B[j];
    sort(S, S + c);
    long long cnt = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        pair<int *, int *> p = equal_range(S, S + c, -C[i] - D[j]);
        cnt += p.second - p.first;
      }
    printf("%lld\n", cnt);
    if (T) puts("");
  }
  return 0;
}
/*
算法分析请参考: 《入门经典 第2版》例题8-3
注意如何通过对equal_range的一次调用获得目标区间的两个端点。
*/
// 25887846	1152	4 Values whose Sum is 0	Accepted	C++	1.910
// 2020-12-26 05:37:35